package com.updata.up.utils;

import org.apache.commons.io.IOUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
//获取jar版本号
public class jarFileUtils {
    /**
     * 获取本地版本号
     * @return 返回版本信息
     * @throws IOException
     */
    public static String readJarFile(String pathStr) throws IOException {
        String versionStr="";
        //得到路径（开发环境没有.jar；jar包中存在.jar）
//        String pathStr =this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
//        String pathStr ="D:\\idea\\up\\target\\up-0.1.jar";
        pathStr = decodeStr(pathStr);//编码为utf-8
        if(pathStr.indexOf(".jar") == -1){
            //此为开发环境
            versionStr = getPomVersion();
        }else{
            //jar包
            versionStr=getJarVersion(pathStr);
        }
       return versionStr;
    }
    private static String getPomVersion() throws UnsupportedEncodingException {
        String versionStr = "";
        //项目路径
        String pathStr = System.getProperty("user.dir");
        MavenXpp3Reader mx3Reader = new MavenXpp3Reader();
        String pomPath = pathStr + File.separator+"pom.xml";
        pomPath = decodeStr(pomPath);//编码为utf-8
        try{
            Model model = mx3Reader.read(new FileReader(pomPath));
            versionStr = model.getVersion();
        }catch(Exception e){
            e.printStackTrace();
        }
        return versionStr;
    }
    private static String getJarVersion(String pathStr) throws IOException {
        String versionStr = "";
        JarFile jarFile = null;
        try{
            pathStr = pathStr.replace("!//BOOT-INF/classes!/","");
            pathStr = decodeStr(pathStr);
            if(pathStr.startsWith("file:")){
                pathStr = pathStr.substring(5);
            }
            jarFile = new JarFile(pathStr);
            JarEntry jarEntry = jarFile.getJarEntry("META-INF/maven/com.updata/up/pom.properties");
            if(jarEntry != null){
                StringWriter sw = new StringWriter();
                FileCopyUtils.copy(new InputStreamReader(jarFile.getInputStream(jarEntry)),sw);
                String text = IOUtils.toString(new StringReader(sw.toString()));
                String[] splitStrs = text.split("\r\n");
                for(String splitStr:splitStrs){
                    if(splitStr.contains("version")){
                        versionStr=splitStr.substring(splitStr.indexOf("=")+1).trim();
                    }
                }
            }

        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                jarFile.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return versionStr;
    }
    public static String decodeStr(String text) throws UnsupportedEncodingException {
        try{
            text = java.net.URLDecoder.decode(text,"UTF-8");
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return text;
    }

}
